<?php
namespace Admin\Controller;
use Think\Controller;
class PublicController extends Controller {

	//新增公众号
   //新增公众号
    public function index(){
        $data['name']    = I('post.name');                  //公众号名称
        $data['type_id'] = I('post.category');                    //公众号类型id
        $data['id']      = I('post.channel');                    //渠道id
        $data['details'] = I('post.details');                  //公众号简介
        $data['public']  = I('post.number');                      //微信公众号
        $data['label_name']   = I('post.label');                  //标签名
        $data['selected']   = I('post.selected');             //是否精选
        $data['head_pic']   = I('post.head_pic');             //是否精选
        $data['brand_pic']   = I('post.brand_pic');             //是否精选


        $arr = M('wechart_public')->select();
        foreach($arr as $v){
            $rank = $v['id']+1;
        }
        $data['rank']  = $rank;


        if(1){
            $insert_pub = M('wechart_public')->add($data);
            if($insert_pub){
                $ajaxReturn['code'] = '1';
                $ajaxReturn['msg']  = 'SUCCESS';
                $ajaxReturn['data']  = $data;
                $this->ajaxReturn($ajaxReturn);
            }else{
                $ajaxReturn['code'] = '2';
                $ajaxReturn['msg']  = 'FAIL';
                $ajaxReturn['data'] = '添加失败';
                $this->ajaxReturn($ajaxReturn);
            }
        }else{
            $ajaxReturn['code'] = '2';
            $ajaxReturn['msg']  = 'FAIL';
            $ajaxReturn['data'] = '不能有空数据';
            $this->ajaxReturn($ajaxReturn);
        }
    }



    //展示公众号分类
    public function publicType(){

    }


//公众号列表
    public function lists(){
         
       $type_id      = I('get.type_id');

       $public_lists = M('wechart_public');
        //查询数据总条数
       $number      = $public_lists->alias('a')->join('table_channel b ON b.id= a.id')->join('table_public_type c ON c.type_id= a.type_id')->field('a.name,details,a.public_id,a.rank,public,type_name,corporate_name,pian,first_num,average_num,like,effect,selected')->COUNT();

        //设置每页条数
        $page_size = I('get.page_size');
        //计算可以分为多少页
        $page_count = ceil($number/$page_size);
        //接收当前页
        $page     =  I('get.page');
        
        $start    = ($page-1)*$page_size;

        //公众号与所属渠道公司查询
        if(empty($type_id)){
            $pub_list      = $public_lists->alias('a')->join('table_channel b ON b.id= a.id')->join('table_public_type c ON c.type_id= a.type_id')->field('a.name,details,a.public_id,a.rank,public,type_name,corporate_name,pian,first_num,average_num,like,effect,selected')->order('rank desc')->limit($start,$page_size)->select();
        }else{
            $pub_list      = $public_lists->alias('a')->join('table_channel b ON b.id= a.id')->join('table_public_type c ON c.type_id= a.type_id')->field('a.name,details,a.public_id,c.type_id,a.rank,public,type_name,corporate_name,pian,first_num,average_num,like,effect,selected')->where("c.type_id=$type_id")->order('rank desc')->limit($start,$page_size)->select();
        }


        if($pub_list){
            $data['page']['total_page'] = $page_count;
            $data['list'] = $pub_list;
            $data['lists'] = $page;
            

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
            $this->ajaxReturn($ajaxReturn);
        }
    }



    //展示修改公众号页面
    public function update(){
    	$public = I('get.public');
    	if(!empty($public)){
    		//公众号表与渠道表联合查询展示信息
    		$pub_list      = M('wechart_public')->alias('a')->join('table_channel b ON b.id= a.id')->join('table_public_type c ON c.type_id= a.type_id')->field('a.name,details,a.public_id,public,label_name,type_name,corporate_name,selected,head_pic,brand_pic')->where("public_id=$public")->find();
    		if(!empty($pub_list)){

	    		$ajaxReturn['code'] = '1';
				$ajaxReturn['msg']  = 'SUCCESS';
				$ajaxReturn['data'] = $pub_list;
				$this->ajaxReturn($ajaxReturn);
	    	}else{
	    		$ajaxReturn['code'] = '2';
				$ajaxReturn['msg']  = 'FAIL';
				$this->ajaxReturn($ajaxReturn);
	    	}
    	}else{
	    		$ajaxReturn['code'] = '2';
				$ajaxReturn['msg']  = 'FAIL';
				$ajaxReturn['data'] = '公众号id为空';
				$this->ajaxReturn($ajaxReturn);
	    }
    }



    //修改公众号
    public function update_pro(){
    	$public_id 		 = I('post.id');
    	 $data['name']    = I('post.name');                  //公众号名称
        $data['type_id'] = I('post.category');                    //公众号类型id
        $data['id']      = I('post.channel');                    //渠道id
        $data['details'] = I('post.details');                  //公众号简介
        $data['public']  = I('post.number');                      //微信公众号
        $data['label_name']   = I('post.label');                  //标签名
        $data['selected']   = I('post.selected');             //是否精选
        $data['head_pic']   = I('post.head_pic');             //是否精选
        $data['brand_pic']   = I('post.brand_pic');             //是否精选


   		$update_pub = M('wechart_public')->where("public_id=$public_id")->save($data);
		if($update_pub){
			$ajaxReturn['code'] = '1';
			$ajaxReturn['msg']  = 'SUCCESS';
			$this->ajaxReturn($ajaxReturn);
		}else{
			$ajaxReturn['code'] = '2';
			$ajaxReturn['msg']  = 'FAIL';
			$ajaxReturn['data'] = '修改失败';
			$this->ajaxReturn($ajaxReturn);
		}
    }



    //删除公众号操作
    public function delete(){
    	$public_id 		 = I('get.public');       //公众号id
    	$delete_pub = M('wechart_public')->where("public_id=$public_id")->delete();
		if($delete_pub){       //如果成功返回200，如果是失败返回10007
			$ajaxReturn['code'] = '1';
			$ajaxReturn['msg']  = 'SUCCESS';
			$this->ajaxReturn($ajaxReturn);
		}else{
			$ajaxReturn['code'] = '2';
			$ajaxReturn['msg']  = 'FAIL';
			$ajaxReturn['data'] = '删除失败';
			$this->ajaxReturn($ajaxReturn);
		}
    }

    //所属渠道
    public function channel(){
        $channel = M('channel')->field('id,corporate_name')->select();
        if($channel){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $channel;
            $this->ajaxReturn($ajaxReturn);
        }
    }

    //所属分类
    public function type(){
        $type = M('public_type')->field('type_id,type_name')->select();
        if($type){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $type;
            $this->ajaxReturn($ajaxReturn);
        }
    }
    

    /*
     * 排序
     */
    public function rank (){

        $thisid = I('post.id');
        $type   = I('post.type');
        $and    = $type == 1 ? '>' : '<';
        $order  = $type == 1 ? 'asc' : 'desc';

        $ArticleModel = M('wechart_public');

        $thisinfo   = $ArticleModel->where('public_id='.$thisid)->getField('rank,name');
        $thisrank   = array_keys($thisinfo)[0];
        //$thiscid    = array_values($thisinfo)[0];


        $nextinfo   = $ArticleModel->where('rank'.$and.$thisrank)->order('rank '.$order)->getField('public_id,rank');
        
        $nextid     = array_keys($nextinfo)[0];
        $nextrank   = array_values($nextinfo)[0];

        $ajaxReturn['data'] = $nextinfo;

        if($thisrank && $nextid && $nextrank){

            $ArticleModel-> where('public_id='.$thisid)->setField('rank',$nextrank);
            $ArticleModel-> where('public_id='.$nextid)->setField('rank',$thisrank);

            $data['thisid'] = $thisid;
            $data['thisrank'] = $thisrank;
            $data['nextid'] = $nextid;
            $data['nextrank'] = $nextrank;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据更新失败！";
        }

        $this->ajaxReturn($ajaxReturn);

    }

    //完善公众号信息
    public function complete(){
       
        if($_SERVER['REQUEST_METHOD'] != "POST"){
             $public   =  I('get.id');
             $wechart_public = M('wechart_public')->field('pian,first_num,average_num,like,effect')->where("public_id=$public")->find();
            if($wechart_public){
                $ajaxReturn['code'] = '1';
                $ajaxReturn['msg']  = 'SUCCESS';
                $ajaxReturn['data'] = $wechart_public;
                $this->ajaxReturn($ajaxReturn);
            }else{
                $ajaxReturn['code'] = '2';
                $ajaxReturn['msg']  = 'FAIL';
                $this->ajaxReturn($ajaxReturn);
            }
        }else{
            $public   =  I('post.id');
            $data['pian']        = I('post.distribute');            //公众号发布（次/篇）
            $data['first_num']   = I('post.firstRead');       //头条阅读数
            $data['average_num'] = I('post.averageRead');     //平均阅读数
            $data['like']        = I('post.allLike');            //点赞数
            $data['effect']      = I('post.influence');          //影响力
            if(M('wechart_public')->where("public_id=$public")->save($data)){
                $ajaxReturn['code'] = '1';
                $ajaxReturn['msg']  = 'SUCCESS';
                $this->ajaxReturn($ajaxReturn);
            }else{
                $ajaxReturn['code'] = '2';
                $ajaxReturn['msg']  = '添加失败';
                $this->ajaxReturn($ajaxReturn);
            }
        }
    }


}